/*
 * @Author: zhangyu
 * @Date: 2021-01-15 17:56:00
 * @LastEditTime: 2021-02-24 10:01:21
 */
const BaseService = require("./BaseService.js")
const moment = require('moment')
const md5 = require('md5-node');

class UserService extends BaseService {
  // 登录
  async login(params){
    let result = await this.Db('user').where('username','=',params.username).find();
    if(result){
      let results = await this.Db('user').where('username','=',params.username).where('password','=',md5(params.password)).find();
      if(results){
        // 查询该用户的菜单
        let role = await this.Db('role').where('id','=',results.role_id).find()
        let menu = await this.Db('menu').field('id,uid,en_name').where('id','in',role.menu_ids).select()
        // 整理菜单权限
        let menuArray = []
        menu.forEach(v => {
          menuArray.push(v.en_name)
        })
        return {
          code: 64,
          msg: '登录成功',
          data: {
            id: result.id,
            role_id: result.role_id,
            name: result.name,
            avatar: result.avatar,
            menu: menuArray
          }
        }
      }else{
        return {
          code: 32,
          msg: '密码不正确'
        }
      }
    }else{
      return {
        code: 16,
        msg: '账号不存在'
      }
    }
  }

  // 获取权限
  async getAuth(role_id){
    // 查询该用户的菜单
    let role = await this.Db('role').where('id','=',role_id).find()
    let menu = await this.Db('menu').field('id,uid,en_name').where('id','in',role.menu_ids).select()
    // 整理菜单权限
    let menuArray = []
    menu.forEach(v => {
      menuArray.push(v.en_name)
    })
    return menuArray
  }

  // 用户列表
  async list(params){
    let model = this.Db('user')
    .field('A.id,A.role_id,A.username,A.name,A.avatar,A.create_time,A.update_time,B.name as role_name')
    .alias('A').join('role B','A.role_id = B.id');
    // 账号
    if(params.username){
      model = model.where('A.username','like',`%${params.username}%`)
    }
    // 昵称
    if(params.name){
      model = model.where('A.name','like',`%${params.name}%`)
    }
    // 角色
    if(params.role_id){
      model = model.where('B.id','=',`${params.role_id}`)
    }
    // 创建时间
    if(params.create_time){
      model = model.whereBetweenTime('A.create_time',params.create_time[0],params.create_time[1])
    }
    let result = await model.page(params.current,params.size).select()
    // 总条数
    let totalCount = await model.count()
		return {
      list: result,
      totalCount
    }
  }

  // 创建用户
  async create(params){
    let result = await this.Db('user').where('username','=',params.username).find()
    if(typeof result == 'object'){
      return false
    }else{
      this.Db('user').insert({
        username: params.username,
        password: md5(params.password),
        name: params.name,
        avatar: 'http://qo95rwcqv.hb-bkt.clouddn.com/avatar.jpg',
        role_id: params.role_id,
        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        update_time: moment().format('YYYY-MM-DD HH:mm:ss')
      });
      return true
    }
  }

  // 编辑用户
  edit(params){
    this.Db('user').where('id','=',params.id).update({
      role_id: params.role_id,
      name: params.name,
      update_time: moment().format('YYYY-MM-DD HH:mm:ss')
    });
    return true
  }

  // 删除
  deleteItem(params){
    this.Db('user').where('id','=',params.id).delete()
    return true
  }

  // 重置密码
  async resetPassword(params){
    let ID = ''
    if(params.id){
      ID = params.id
    }else{
      let user = await this.ctx.helper.getUser()
      ID = JSON.parse(user).id
    }
    this.Db('user').where('id','=',ID).update({
      password: md5(params.password)
    })
    return true
  }
}

module.exports = UserService;